# registering the spreadsheet

cville_sheets <- gs_url("https://docs.google.com/spreadsheets/d/1ISo4boBzGck4N8Jje_kDKCM8eVP6ftLCbXGbnHCx-f8/edit#gid=0")
## Sheet-identifying info appears to be a browser URL.
## googlesheets will attempt to extract sheet key from the URL.
## Putative key: 1ISo4boBzGck4N8Jje_kDKCM8eVP6ftLCbXGbnHCx-f8
## Worksheets feed constructed with public visibility
cville_tracts <- tracts(state = "VA", county = c("Albemarle", "Fluvanna", "Greene", "Nelson", "Charlottesville city"))

cville_race <- cville_sheets %>%
  gs_read(ws= "geo_race")
## Accessing worksheet titled 'geo_race'.
## Parsed with column specification:
## cols(
##   Id = col_character(),
##   Id2 = col_double(),
##   Geography = col_character(),
##   N_total = col_integer(),
##   P_total = col_integer(),
##   N_white = col_integer(),
##   P_white = col_double(),
##   N_black = col_integer(),
##   P_black = col_double(),
##   N_AmIn = col_integer(),
##   P_AmIn = col_double(),
##   N_asian = col_integer(),
##   P_asian = col_double(),
##   N_hawaiian = col_integer(),
##   P_hawaiian = col_double(),
##   N_other = col_integer(),
##   P_other = col_double(),
##   N_hispanic = col_integer(),
##   P_hispanic = col_double()
## )
cville_race_geo <- geo_join(cville_tracts, cville_race, by_sp = "GEOID", by_df = "Id2", how = "inner")
pal <- colorBin("PuBu", cville_race_geo$P_white, 6)

leaflet(cville_race_geo) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~pal(P_white),
    fillOpacity = 0.4,
    color = "Black",
    weight = 0.7
  ) %>%
  addLegend(
    position = "bottomright",
    pal = pal,
    values = ~P_white,
    labFormat = labelFormat(suffix = "%"),
    title = "Percent White"
  ) %>%
  addMiniMap(
    tiles = providers$CartoDB.Positron,
    position="topright",
    toggleDisplay = TRUE
  )
make_map <- function(variable_name, race) {
  pal <- colorBin("PuBu", variable_name, 4)

  m <- leaflet(cville_race_geo) %>%
    addProviderTiles(providers$CartoDB.Positron) %>%
    addPolygons(
      fillColor = ~pal(variable_name),
      fillOpacity = 0.4,
      color = "Black",
      weight = 0.7
    ) %>%
    addLegend(
      position = "bottomright",
      pal = pal,
      values = ~variable_name,
      labFormat = labelFormat(suffix = "%"),
      title = paste0("Percent ", race)
    ) %>%
    addMiniMap(
      tiles = providers$CartoDB.Positron,
      position="topright",
      toggleDisplay = TRUE
    )
  return(m)
}
white <- make_map(variable_name = cville_race_geo$P_white, race = "White")

white
black <- make_map(variable_name = cville_race_geo$P_black, race = "Black")

black
hispanic <- make_map(variable_name = cville_race_geo$P_hispanic, race = "Hispanic")

hispanic
asian <- make_map(variable_name = cville_race_geo$P_asian, race = "Asian")

asian
pacific_is <- make_map(cville_race_geo$P_hawaiian, "Hawaiian/Pacific Islander")

pacific_is
native <- make_map(cville_race_geo$P_AmIn, "American Indian/Alaska Native")

native
other <- make_map(cville_race_geo$P_other, "Other")

other
cville_dissimilarity <- cville_sheets %>%
  gs_read(ws = 2)
## Accessing worksheet titled 'dissimilarity_index'.
## Parsed with column specification:
## cols(
##   Race = col_character(),
##   Dissimilarity_index = col_double(),
##   Proportion = col_double(),
##   Total = col_integer()
## )
cville_dissimilarity$Race <- factor(cville_dissimilarity$Race, levels = c("White", "Black", "Hispanic", "Asian", "Other", "American Indian/Alaska Native", "Hawaiian/Pacific Islander"))

repres <- ggplot(cville_dissimilarity, aes(x = Race, y = Proportion)) +
  geom_bar(stat = "identity", fill = "#f27059") + theme_hc() +
  theme(axis.text.x = element_text(angle = 70, hjust = 1)) + 
  ggtitle("Racial/Ethnic Representation") + 
  theme(title = element_text(color = "#6f6f70", size = 15), axis.title = element_text(color = "#6f6f70", size = 12), axis.text = element_text(color = "#6f6f70")) + 
  xlab(NULL) +
  geom_text(aes(x = Race, y = Proportion, label= Proportion), vjust=-0.8, color = "#6f6f70") + 
  ylim(0,1)

repres

ggsave( "representation.png", plot = repres)
## Saving 7 x 5 in image
cville_dissimilarity$Race <- factor(cville_dissimilarity$Race, levels = c("Asian", "Other", "Black", "White", "Hawaiian/Pacific Islander", "Hispanic", "American Indian/Alaska Native"))

ggplot(cville_dissimilarity, aes(x = Race, y = Dissimilarity_index)) +
  geom_bar(stat = "identity", fill = "#f27059") + theme_hc() +
  theme(axis.text.x = element_text(angle = 70, hjust = 1)) + 
  ggtitle("Dissimilarity Indices") + geom_text(aes(label= Dissimilarity_index), vjust=-0.2)

ggplot(cville_dissimilarity) +
  geom_segment(aes(x=Race, y=Dissimilarity_index, xend=Race, yend=0), size = 1.1, color = "#6f6f70") + 
  geom_point(aes(x=Race, y=Dissimilarity_index), size = 3.7, color = "#f27059") +
  theme(axis.text.x = element_text(angle = 70, hjust = 1)) +
  labs(x = NULL, y = "Dissimilarity index value", title = "Dissimilarity indices in Charlottesville, VA") + 
  theme_hc() +
  theme(title = element_text(color = "#6f6f70", size = 15), axis.title = element_text(color = "#6f6f70", size = 12), axis.text = element_text(color = "#6f6f70")) + 
  geom_text(aes(x = Race, y = Dissimilarity_index, label= Dissimilarity_index), vjust=-0.8, color = "#6f6f70") +
  ylim(0, 60) 

ggsave("lolipop_index.png")
## Saving 7 x 5 in image
group_quarters <- read_csv("group_quarters.csv")
## Parsed with column specification:
## cols(
##   .default = col_character()
## )
## See spec(...) for full column specifications.
colnames(group_quarters) <- group_quarters[1,]

colnames(group_quarters)[4] <- "Total"

group_quarters$Total <- as.numeric(group_quarters$Total)
## Warning: NAs introduced by coercion
group_quarters <- group_quarters[-1,]

group_quarters_geo <- geo_join(cville_tracts, group_quarters, by_sp = "GEOID", by_df = "Id2", how = "inner")
pal <- colorBin(palette = "PuBu", domain = group_quarters_geo$Total, 3)

leaflet(group_quarters_geo) %>%
    addProviderTiles(providers$CartoDB.Positron) %>%
   addPolygons(
      fillColor = ~pal(as.numeric(group_quarters_geo$Institutionalized.population..101.106..201.203..301..401.405.....Correctional.facilities.for.adults..101.106..)),
      fillOpacity = 0.4,
      color = "Black",
      weight = 0.7
  ) %>%
    addLegend(
      position = "bottomright",
      pal = pal,
      values = ~as.numeric(group_quarters_geo$Institutionalized.population..101.106..201.203..301..401.405.....Correctional.facilities.for.adults..101.106..),
      title = "Population in Correctional Facility"
    ) %>%
    addMiniMap(
      tiles = providers$CartoDB.Positron,
      position="topright",
      toggleDisplay = TRUE
    )